﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace app.Api
{
    /*
        公用接口
    */
    public class ApiEmail : ApiBase
    {
        public ApiEmail(Controller ctx) : base(ctx) { }
        
        public void Do(string action)
        {
            switch (action)
            {
                case "get_list":
                    {
                        List<Email> emailList = G.DB.Get<Email>().GetFirstTableList();
                        ResponseJson(new ListResult<Email> {list =emailList});
                        break;
                    }
                case "delete_email":
                    {
                        CheckPermission(IDENTITY.ADMIN,Permission.Module(PERMISSIONMODULE.EMAIL_VALUE).Delete());
                        string id = GetStringParam(new ReqStringParamOption { Name = "the_delete_email_id" });
                        int value = G.DB.Delete<Email>(id);
                        if (value != 0)
                        {
                            List<Email> emailList = G.DB.Get<Email>().GetFirstTableList();
                            if(emailList.Count !=0)
                            {
                                emailList[0].selected = 1;
                                G.DB.Update<Email>(emailList[0]);
                            }
                            ResponseJson(new BooleanResult { message = "删除成功" });
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { message = "删除失败" });
                        }
                        break;
                    }
                case "create_new_email":
                    {
                        CheckPermission(IDENTITY.ADMIN,Permission.Module(PERMISSIONMODULE.EMAIL_VALUE).Add());
                        Email email = new Email();
                        ApplyEmailValue(email);
                        List<Email> email_list = G.DB.Get<Email>().GetFirstTableList();
                        if (email_list.Count == 0)  //默认将第一条数据设为选中
                        {
                            email.selected = 1;
                        }
                        else
                        {
                            email.selected = 0;
                        } 
                        bool flag = false;   //表示当前数据中没有要添加的邮箱
                        foreach (var item in email_list)
                        {
                            if (item.email_name == email.email_name && item.email_password == email.email_password && item.email_authorize_code == email.email_authorize_code && item.smtp == email.smtp && item.email_port == email.email_port)
                            {
                                flag = true;
                            }

                        }
                        if (!flag)
                        {
                            int value = G.DB.Insert<Email>(email);
                            if (value != 0)
                            {
                                ResponseJson(new BooleanResult { value = true, message = "邮箱添加成功！" });
                            }
                            else
                            {
                                ResponseJson(new BooleanResult { value = false, message = "邮箱添加失败！" });
                            }
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { value = false, message = "已经含有该条信息" });
                        }
                        break;
                    }
                case "change_email":
                    {
                        CheckPermission(IDENTITY.ADMIN,Permission.Module(PERMISSIONMODULE.EMAIL_VALUE).Modify());
                        Email email = G.DB.GetById<Email>(req_id);
                        Email tempEmail = new Email();
                        tempEmail.email_name = email.email_name;
                        tempEmail.email_authorize_code = email.email_authorize_code;
                        tempEmail.email_password = email.email_password;
                        tempEmail.email_port = email.email_port;
                        tempEmail.smtp = email.smtp;
                        ApplyEmailValue(email);
                        if (tempEmail.email_name == email.email_name && tempEmail.email_password == email.email_password && tempEmail.email_port == email.email_port && tempEmail.email_authorize_code == email.email_authorize_code && tempEmail.smtp == email.smtp)
                        {
                            ResponseJson(new BooleanResult { message = "已经含有该条信息" });
                        }
                        else
                        {
                            int value = G.DB.Update<Email>(email);
                            if (value != 0)
                            {
                                ResponseJson(new BooleanResult { message = "邮箱修改成功" });
                            }
                            else
                            {
                                ResponseJson(new BooleanResult { message = "邮箱修改失败" });
                            }
                        }
                        break;
                    }
                case "selsct_email":
                    {
                        CheckPermission(IDENTITY.ADMIN,Permission.Module(PERMISSIONMODULE.EMAIL_VALUE).STARTUSING());
                        int value1 = 0;
                        int oldSelectedEmailId = 0;
                        Cd cd = new Cd();
                        cd.And("selected ='{0}'",1);
                        List<Email> list = GetListResult<Email>(cd.Render()).list;
                        foreach (var item in list)
                        {
                            oldSelectedEmailId = item.id;
                            item.selected =0;
                            value1 =G.DB.Update<Email>(item);
                        }
                        Email email =  G.DB.GetById<Email>(req_id);
                        email.selected = 1;
                        int value2 = G.DB.Update<Email>(email);
                        if (value2 != 0 && value1 != 0)
                        {
                            ResponseJson(new BooleanResult { message = "启用成功"});
                        }
                        else
                        {
                            ResponseJson(new BooleanResult { message = "启用失败" });
                        }
                        break;
                    }
                default:break;
            }
        }

        private void ApplyEmailValue(Email email)
        {
            email.email_name = GetStringParam(new ReqStringParamOption { Name = "email_name" });
            email.email_password = GetStringParam(new ReqStringParamOption { Name = "email_password" });
            email.email_port = GetStringParam(new ReqStringParamOption { Name = "email_port" });
            email.smtp = GetStringParam(new ReqStringParamOption { Name = "email_smtp" });
            email.email_authorize_code = GetStringParam(new ReqStringParamOption { Name = "email_authorize_code" });
        }
    }
}